package com.project.shortlink.advice;

import com.project.shortlink.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice
@Slf4j
@ResponseBody
public class MyExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public R catchCustomException(CustomException e){
        log.error(e.getMessage());
        return R.error(e.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    public R catchException(Throwable e){
        log.error("发生了异常",e);
        return R.error("未知异常");
    }
}
